package com.pruebas.physics.simplegravity;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView;
import android.opengl.GLU;

public class SimpleGravityRenderer implements GLSurfaceView.Renderer {
	
	private static final float initialYPos = -2f;
	private static final float initialYSpeed = 1f;
	private static final float gravity = -0.2f;
	
	private FloatBuffer rectangleVB;
	
	private float time = 0f;
	
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// Set the background frame color
        gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        // initialize the rectangle vertex array
        initShapes();
        
        // Enable use of vertex arrays
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    }
    
    public void onDrawFrame(GL10 gl) {
    	// Redraw background color
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        
        // Set GL_MODELVIEW transformation mode
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();   // reset the matrix to its default state
        
        // When using GL_MODELVIEW, you must set the view point
        GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
        
        
        float yPos = initialYPos + initialYSpeed * time + 0.5f * gravity * (time * time);
        gl.glTranslatef(0f, yPos, 0f);
        time += 0.5f;
        
        // Draw the rectangle
        gl.glColor4f(1f, 1f, 1f, 0.0f);
        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, rectangleVB);
        gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);
        
    }
    
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        
        // make adjustments for screen ratio
        float ratio = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);        // set matrix to projection mode
        gl.glLoadIdentity();                        // reset the matrix to its default state
        gl.glFrustumf(-ratio, ratio, -1, 1, 1f, 10);  // apply the projection matrix

    }
    
    private void initShapes(){
        
        float rectangleCoords[] = {
    		-0.5f,	1f,
            -0.5f,	0f,
             0.5f,	0f,
             0.5f,	1f
        };
        
        // initialize vertex Buffer for triangle  
        ByteBuffer vbb = ByteBuffer.allocateDirect(
                // (# of coordinate values * 4 bytes per float)
                rectangleCoords.length * 4); 
        vbb.order(ByteOrder.nativeOrder());// use the device hardware's native byte order
        rectangleVB = vbb.asFloatBuffer();  // create a floating point buffer from the ByteBuffer
        rectangleVB.put(rectangleCoords);    // add the coordinates to the FloatBuffer
        rectangleVB.position(0);            // set the buffer to read the first coordinate
    
    }

}
